import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.net.URI;

public class DemoFour {
    public static void main(String[] args) {
        System.setProperty("hadoop.home.dir", "D:\\code2\\hadoop-2.7.3");
        System.setProperty("HADOOP_USER_NAME", "root");

        try {

        String url = "hdfs://192.168.75.11:9000";
        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(URI.create(url), conf);

        //案例4：修改文件名
        //源文件名字
        //Path srcName = new Path("/test/17demo3.txt");
        //目标文件名字
        //Path destName = new Path("/test/17deo3New.txt");
        //rename修改文件名字，返回true或者false
        //boolean f = fileSystem.rename(srcName, destName);
        //用了三目运算符，true打印第一个值，false打印第二个值
        //System.out.println(f == true ? "更名成功" : "更名不成功");

        //案例5：删除
        //Path path = new Path("/test/17.txt");
        //boolean f = fileSystem.delete(path);
        //System.out.println(f == true? "删除成功": "删除失败");

        //案例6：创建文件夹
        //Path path = new Path("/test/17test");
        //boolean f = fileSystem.mkdirs(path);
        //System.out.println(f == true? "创建成功": "创建失败");

        //案例7：查看文件列表内容状态
        String urlNew = "hdfs://192.168.75.11:9000/test";
        FileStatus[] fileStatuses = fileSystem.listStatus(new Path(urlNew));

        for(FileStatus fileStatus:fileStatuses){
            if(fileStatus.isFile()){
                System.out.println("file:"+fileStatus.getPath().toString());
            }
            else{
                System.out.println("directory:"+fileStatus.getPath().toString());
            }
        }


        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
